import React from 'react'
import {Row, Col, Switch, Button, message} from 'antd'
import {LeftOutlined} from '@ant-design/icons';
import {PoweroffOutlined} from '@ant-design/icons';
import api from "../../../api/api"
import './styles.css'
import axios from "axios";

class ConfigurationCenter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ocr: {
                enable: false
            },
            loadings: []
        }
    }

    componentDidMount() {
        console.log("初始化");
        this.queryAllSystemConfig();
    }

    queryAllSystemConfig = () => {
        let that = this;
        let url = api.api_URL + "/admin/sysconfig/queryAllSystemConfigByOrgCode?orgCode=" + localStorage.getItem('orgCode');
        axios.post(url, {}, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            console.log(res.data)
            if (res.data.code != 1) {
                alert(res.data.msg);
                return;
            }
            const dataList = res.data.data.systemConfigList;
            let enableFlag = dataList[0].flag == 0 ? false : true;
            that.setState({
                ocr: {
                    enable: enableFlag
                }
            })
        }).catch(function (err) {
            console.log(err);
        });
    }

    onChange = (checked) => {
        let that = this;
        console.log(`switch to ${checked}`);
        let checkedFlag = 0;
        if (checked) {
            checkedFlag = 1;
        }
        let url = api.api_URL + "/admin/sysconfig/updateByOrgcode?orgCode=" + localStorage.getItem('orgCode')+"&flag="+checkedFlag;
        axios.post(url, {
        }, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            console.log(res.data)
            if (res.data.code != 1) {
                alert(res.data.msg);
                return;
            }
            that.setState({
                ocr: {
                    enable: checked
                }
            })
        }).catch(function (err) {
            console.log(err);
        });
    }

    syncData = index => {
        let that = this;
        const syncKey = "syncKey";
        message.loading({content: '增量数据同步中，请稍候', key: syncKey, duration: 5});
        this.setState(({loadings}) => {
            const newLoadings = [...loadings];
            newLoadings[index] = true;
            return {
                loadings: newLoadings,
            };
        });
        let url = api.api_URL + "/admin/syncdata/syncDataFromThirdPart?syncType=0";
        axios.get(url, {}, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            console.log(res.data)
            if (res.data.code != 1) {
                alert(res.data.msg);
                return;
            }
            message.success({content: "增量数据同步完成", key: syncKey, durationL: 5})
            that.setState({
                loadings: false,
            })
        }).catch(function (err) {
            console.log(err);
            message.error({content: "增量数据同步失败", key: syncKey, durationL: 5})
        });
    }

    render() {
        const {loadings} = this.state;
        return (
            <div className="girdBg">
                <div>
                <Row className="searchBox">
                    <Col span={20}>
                        身份验证OCR接口
                    </Col>
                    <Col span={4} className="btnRight">
                        <Switch checked={this.state.ocr.enable} onChange={this.onChange}/>
                    </Col>
                </Row>
                </div>
                <div className={localStorage.getItem('orgCode') === "base_chzy" ? "elementShow" : "elementHidden"}>
                <Row className="searchBox">
                    <Col span={20}>
                        手动增量同步数据
                    </Col>
                    <Col span={4} className="btnRight">
                        <Button
                            type="primary"
                            icon="{<PoweroffOutlined/>}"
                            loading={loadings[1]}
                            onClick={() => this.syncData(1)}
                        >
                            同步
                        </Button>
                    </Col>

                </Row>
                </div>
            </div>
        )
    }
}

export default ConfigurationCenter